('use strict');

import * as mongoose from 'mongoose';
import commonUtil from '@qianmi/x-site-common/util/common-util';
let filePath =
  '//qianmi-web.oss-cn-hangzhou.aliyuncs.com/x-site/' + commonUtil.getEnvCode() + '/public/buildTemplate/${fileName}';
//创建集合模式
let TemplateInfoSchema: mongoose.Schema = new mongoose.Schema({
  templateCode: { type: String, unique: true, required: true }, //模板名称 tplCode 唯一ID
  templateName_cn: { type: String, required: true }, //模板中文名称
  templateName_us: { type: String, default: '' }, //模板英文名称
  detailInfo: {
    //详细信息
    previewImage: String, //预览图 带壳
    previewHtml: String, //预览页面
    detailImage: String, //详图
    author: String, //作者|id
    authorHtml: String, //作者主页
    browserSupport: {}, //当前模板浏览器支持情况
    desc: { type: String, default: '' } //当前模板的详细表述
  },
  isShow: { type: Boolean, default: true }, //模板是否展示
  keyword: [String], //关键字
  updateTime: { type: Date }, //更新时间
  rule4PageCode: {
    type: String,
    required: true,
    enum: ['multi', 'single', 'diy'],
    default: 'multi'
  }, //新建页面 pageCode生成规则
  pageType: { type: String, required: true, default: 'index' }, //模板的type
  platform: { type: String, required: true, enum: ['pc', 'weixin', 'app', 'pad'] }, //模板适用平台
  systemCode: {
    type: String,
    required: true,
    enum: ['d2cStore', 'd2p', 'ehome']
  }, //模板所属业务线
  publishType: {
    type: String,
    enum: ['index', 'index-all', 'subs', 'goodsList', 'goodsInfo']
  },
  styleType: {
    type: String,
    default: 'none',
    enum: ['none', 'standard', 'adapter']
  }, //模板的风格类型
  color2Class: {},
  acceptStyles: [
    {
      name: String,
      title: String,
      background: String,
      defaultSelected: { type: Boolean, default: false },
      color: {}
    }
  ], //模板可配的风格信息
  isDoublePlatformTpl: { type: Boolean, default: false }, //是否是双平台模板
  loadingWidgetNs: { type: String, default: '' },
  acceptBars: [String],
  pluginBars: [String],
  adapterBars: [String],
  dependenciesBar: [String],
  barDefaultPropsMap: {},
  nsTransferMap: {},
  children: [],
  templateConfig: {}
});

//用虚拟字段做兼容处理
TemplateInfoSchema.virtual('id').get(function() {
  return this._id;
});
//用虚拟字段做兼容处理
TemplateInfoSchema.virtual('templateName').get(function() {
  return this.templateCode;
});
//用虚拟字段做兼容处理
TemplateInfoSchema.virtual('platForm').get(function() {
  return this.platform;
});
//文件位置是固定规则生成的 用虚拟属性返回就行了
TemplateInfoSchema.virtual('jsFile').get(function() {
  return filePath.replace('${fileName}', this.templateCode + '.js');
});
//文件位置是固定规则生成的 用虚拟属性返回就行了
TemplateInfoSchema.virtual('cssFile').get(function() {
  return filePath.replace('${fileName}', this.templateCode + '.css');
});
//确保查询结果以json返回时包含虚拟字段
TemplateInfoSchema.set('toJSON', { getters: true, virtual: true });

export default TemplateInfoSchema;
